/*
 * Principal.java
 *
 * Created on 5 de Outubro de 2007, 11:22
 */

package cadastro;

import bd.Database;
import classes.Reserva;
import classes.Util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import javax.swing.*;
import java.util.ArrayList;
import javax.swing.text.MaskFormatter;
import salas.*;
import classes.data;

/**
 *
 * @author  Juliano
 */
public class cadReservas extends javax.swing.JFrame {
    
    /** Creates new form Principal */
    
   ArrayList salas;
   ArrayList clientes;
   AddCliente addCliente;
   static cadReservas p;
    private Database bd;
    public cadReservas(Database meuBanco) {
        super("..:: CONTROLE DE RESERVAS ::..");
        initComponents();
        this.bd = meuBanco;
        r = new Reserva(bd);
        salas = new ArrayList();  
        jList2.setEnabled(false);
        listasessao = new DefaultListModel();
        listafilme = new DefaultListModel();
        listasala = new DefaultListModel();
        mostraDados();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" C�digo Gerado ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jlclientes = new javax.swing.JList();
        botaolocalizar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        try
        {
            formatocpf = new MaskFormatter("###.###.###-##");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"formato errado");
        }
        jtfcpf = new JFormattedTextField(formatocpf);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 2, 18));
        jLabel1.setText("         Filmes");

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Sess\u00e3o");

        jList1.setBackground(new java.awt.Color(204, 204, 204));
        jList1.setBorder(new javax.swing.border.MatteBorder(null));
        jList1.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jList1MouseClicked(evt);
            }
        });

        jScrollPane2.setViewportView(jList1);

        jList2.setBackground(new java.awt.Color(204, 204, 204));
        jList2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jList2.setFont(new java.awt.Font("Dialog", 0, 12));
        jList2.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jList2MouseClicked(evt);
            }
        });

        jScrollPane3.setViewportView(jList2);

        jList3.setBackground(new java.awt.Color(204, 204, 204));
        jList3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane4.setViewportView(jList3);

        jLabel3.setFont(new java.awt.Font("Bitstream Charter", 1, 14));
        jLabel3.setText("Sala");

        jLabel4.setText("CPF");

        jlclientes.setBackground(new java.awt.Color(204, 204, 204));
        jlclientes.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane5.setViewportView(jlclientes);

        botaolocalizar.setText("Localizar cliente");
        botaolocalizar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                botaolocalizarActionPerformed(evt);
            }
        });

        jButton1.setText("Reservar");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 767, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jLabel3)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                                .addGap(22, 22, 22))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(4, 4, 4)
                                        .addComponent(jtfcpf, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE))
                                    .addComponent(botaolocalizar, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(19, 19, 19)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jtfcpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaolocalizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        int pos = s.posicao();
        //JOptionPane.showMessageDialog(null,pos);
        boolean v=true;
        for(int i=0;i<cadeirasR.size();i++){
            if(cadeirasR.get(i).equals(pos)){
                v = false;
            }
        }
        if(v){
            r.setnCadeira(pos);
            try
            {
                 new ListaReserva(bd,r).setVisible(true);
            } catch (ParseException ex)
            {
                 ex.printStackTrace();
            }
        }
        else{
            
            JOptionPane.showMessageDialog(null,"Poltrona reservada");
        }    
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void botaolocalizarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botaolocalizarActionPerformed
    {//GEN-HEADEREND:event_botaolocalizarActionPerformed
// TODO adicione seu c�digo de manipula��o aqui:
        pesquisa();
        
    }//GEN-LAST:event_botaolocalizarActionPerformed

    private void jList2MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jList2MouseClicked
    {//GEN-HEADEREND:event_jList2MouseClicked
// TODO adicione seu c�digo de manipula��o aqui:
        jList3.setEnabled(true);
        id_sala.clear();
        sala.clear();
        listasala.clear();
  
        int i = jList2.getSelectedIndex();
        r.setid_sessao(id_sessao.get(i));
        String sql ="SELECT * FROM sala JOIN sessao ON sala.id_sala = sessao.id_sala AND sessao.id_sessao = "+id_sessao.get(i)+"";
       
        ResultSet rs = bd.executeQueryRs(sql);
        if(bd.Error){
            JOptionPane.showMessageDialog(null,bd.ErrorMessage);
        }
        try
        {
            rs.next();
            listasala.addElement(rs.getString("nome"));
            r.setsala(rs.getString("nome"));
            int tam = Integer.parseInt(rs.getString("n_cadeiras"))*Integer.parseInt(rs.getString("n_cadeiras2"));
            String nome = rs.getString("nome");
            s = new Sala(tam,nome);
            Preenchepoltronas();
            salas.add(s);
            jScrollPane1.setViewportView(s);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        jList3.setModel(listasala);
       
    }//GEN-LAST:event_jList2MouseClicked

    private void jList1MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jList1MouseClicked
    {//GEN-HEADEREND:event_jList1MouseClicked
// TODO adicione seu c�digo de manipula��o aqui:
        jList2.setEnabled(true);
        String f = jList1.getSelectedValue().toString();
        Util aux = new Util();
        r.setfilme(f);
        id_sessao.clear();
        sessao.clear();
        listasessao.clear();
        int l = aux.posicao(f,filme);
        
      
        String sql="SELECT * FROM sessao WHERE (sessao.id_filme = '"+id_filme.get(l) +"')";
          
        ResultSet rs = bd.executeQueryRs(sql);
        if(bd.Error){
           JOptionPane.showMessageDialog(null,bd.ErrorMessage);
           return;
        }
        try
        {
            while (rs.next()){
                id_sessao.add(rs.getString("id_sessao"));
                sessao.add(rs.getString("horario")+"   "+rs.getString("data")+"   "+rs.getString("valor")+" R$");
            }
            for(int i=0;i<id_sessao.size();i++){
               listasessao.addElement(this.sessao.get(i));
            }
            jList2.setModel(listasessao);
        } 
        
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        listasala.clear();
        jList3.removeAll();
    }//GEN-LAST:event_jList1MouseClicked
      
    private void mostraDados()
    {
        data d = new data();
        String date = d.geradata();
        String sql=("SELECT filme.titulo,filme.id_filme FROM filme WHERE (tempo_cartaz+data_estreia > '"+date+"');");
        //JOptionPane.showMessageDialog(null,sql);
        ResultSet rs = bd.executeQueryRs(sql);
        if(bd.Error){
                JOptionPane.showMessageDialog(null,bd.ErrorMessage);
                return;
        }
        try {
            while (rs.next()){
                filme.add(rs.getString("titulo"));
                id_filme.add(rs.getString("id_filme"));
            }
            for(int i=0;i<filme.size();i++){
                listafilme.addElement(this.filme.get(i));
            }
            jList1.setModel(listafilme);
        }    
        catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    private void pesquisa()
    {
        String sql;
        ResultSet rs;
        
        try
        {
            sql= "SELECT cliente.id_cliente, cliente.nome,cliente.cpf, cliente.sexo FROM cliente WHERE (cliente.cpf ='"+jtfcpf.getText()+"')";
            rs = bd.executeQueryRs(sql);
            if(bd.Error){
                JOptionPane.showMessageDialog(null,bd.ErrorMessage);
                return;
            }
            DefaultListModel c = new DefaultListModel();
            int aux = 0;
            rs.next();
            r.setid_cliente(rs.getString("id_cliente"));
            c.addElement("Nome.:"+rs.getString("nome"));
            c.addElement("CPF:"+rs.getString("cpf"));
           
            jlclientes.setModel(c);            
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null,e);
        }
    }
    
    private void Preenchepoltronas()
    {
        String sql = "SELECT * FROM reserva WHERE (reserva.id_sesao = "+r.getid_sessao()+")";
        ResultSet rs = bd.executeQueryRs(sql);
        if(bd.Error)
        {
            JOptionPane.showMessageDialog(null,bd.ErrorMessage);
        }
        try
        {
            while(rs.next()){
                cadeirasR.add(Integer.parseInt(rs.getString("n_cadeira")));
                s.poltronaOcup(Integer.parseInt(rs.getString("n_cadeira")));
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,e);
        }
    }
    
    /**
     * @param args the command line arguments
     */    
    // Declara��o de vari�veis - n�o modifique//GEN-BEGIN:variables
    private javax.swing.JButton botaolocalizar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JList jlclientes;
    private javax.swing.JFormattedTextField jtfcpf;
    // Fim da declara��o de vari�veis//GEN-END:variables
    private Reserva r ;
    private Sala s;
    DefaultListModel listafilme,listasessao,listasala ; 
    private ArrayList <Integer> cadeirasR = new ArrayList <Integer>();
    private ArrayList <String> filme = new ArrayList <String>();
    private ArrayList <String> id_filme = new ArrayList <String>();
    private ArrayList <String> sala = new ArrayList <String>();
    private ArrayList <String> id_sala = new ArrayList <String>();
    private ArrayList <String> sessao = new ArrayList <String>();
    private ArrayList <String> id_sessao = new ArrayList <String>();
    private ArrayList <String> cliente = new ArrayList <String>();
    private ArrayList <String> id_cliente = new ArrayList <String>();
    private MaskFormatter formatocpf;
}
